package com.carrot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.Lists;
import com.carrot.common.core.domain.dto.StationCellReflectionDto;
import com.carrot.common.core.domain.entity.SysStationCellReflection;
import com.carrot.common.core.domain.entity.SysStationCellType;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.mybatis.BaseServiceImpl;
import com.carrot.common.utils.WrapperUtil;
import com.carrot.system.mapper.SysStationCellReflectionMapper;
import com.carrot.system.service.ISysStationCellReflectionService;
import com.carrot.system.service.ISysStationCellTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysStationCellReflectionServiceIImpl
        extends BaseServiceImpl<SysStationCellReflectionMapper, SysStationCellReflection>
        implements ISysStationCellReflectionService {

    @Autowired
    private ISysStationCellTypeService cellTypeService;

    @Override
    public List<SysStationCellReflection> getStationCellReflectionList(String stationId) {

        List<SysStationCellReflection> list = this.query()
                .eq("station_id", stationId)
                .list();

        return list;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void updateCellReflection(StationCellReflectionDto dto) {

        SysStationCellType cellType = cellTypeService.query()
                .eq("id", dto.getCellTypeId())
                .one();
        if (null == cellType) {
            throw new ServiceException("不存在该单体类型");
        }

        int cellTypeNum = cellType.getCellTypeNum();

        List<StationCellReflectionDto.StationCellReflectionItem> list = dto.getList();

        // 按照开始编号排序
        List<StationCellReflectionDto.StationCellReflectionItem> sortedList = list.stream().
                sorted(Comparator.comparing(StationCellReflectionDto.StationCellReflectionItem::getStartNo))
                .collect(Collectors.toList());


        int cellReflectionSize = sortedList.size();

        StationCellReflectionDto.StationCellReflectionItem first = sortedList.get(0);
        StationCellReflectionDto.StationCellReflectionItem end = sortedList.get(cellReflectionSize - 1);

        if (first.getStartNo() < 1) {
            throw new ServiceException("开始编号需要大于1");
        }

        if (first.getStartNo() > first.getEndNo()) {
            throw new ServiceException("开始编号不能大于结束编号");
        }

        if (end.getEndNo() != cellTypeNum) {
            throw new ServiceException("结束编号需要等于该单体类型最大限制");
        }

        List<SysStationCellReflection> insertList = Lists.newArrayList();

        SysStationCellReflection firstCellReflection = WrapperUtil.transform(first, SysStationCellReflection.class);
        firstCellReflection.setStationId(cellType.getStationId());
        firstCellReflection.setCellType(cellType.getCellType());
        firstCellReflection.setCellTypeId(dto.getCellTypeId());

        insertList.add(firstCellReflection);

        // 判断是否连续
        for (int i = 1; i < cellReflectionSize; i++) {
            StationCellReflectionDto.StationCellReflectionItem current = sortedList.get(i);
            if (current.getStartNo() <= 1 || current.getStartNo() <= first.getEndNo() || current.getStartNo() - 1 != first.getEndNo()) {
                throw new ServiceException("单体映射编号不连续或有重合");
            }

            SysStationCellReflection cellReflection = WrapperUtil.transform(current, SysStationCellReflection.class);
            cellReflection.setStationId(cellType.getStationId());
            cellReflection.setCellType(cellType.getCellType());
            cellReflection.setCellTypeId(dto.getCellTypeId());

            insertList.add(cellReflection);
        }

        UpdateWrapper<SysStationCellReflection> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cell_type_id", dto.getCellTypeId());
        this.remove(updateWrapper);

        this.saveBatch(insertList);

    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void delCellReflection(Long cellTypeId) {
        UpdateWrapper<SysStationCellReflection> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cell_type_id", cellTypeId);
        this.remove(updateWrapper);
    }


}
